<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>花卉花木</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
    <link rel="stylesheet" href="${ctxPath}/static/manage/layui/css/layui.css" media="all">
    <script src="${ctxPath}/static/manage/layui/layui.js"></script>
    <script src="${ctxPath}/static/manage/js/jquery.js"></script>
</head>
<body>
<div class="x-nav">
        <span class="nav-breadcrumb">
            <a><cite>后台管理></cite></a>
            <a><cite>网站首页></cite></a>
            <a><cite>花卉花木</cite></a>
        </span>
</div>
<div>
    <blockquote class="layui-elem-quote quoteBox">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" class="layui-input searchContext" placeholder="请输入花卉名称关键字" />
            </div>
            <a class="layui-btn search_btn" data-type="reload">搜索</a>
        </div>
        <div class="layui-inline">
            <a class="layui-btn  addType_btn"><i class="layui-icon">&#xe608;</i>添加花卉</a>
        </div>
        <a class="layui-btn" href="/manage/flowers/exportFlowers"><i class="layui-icon">&#xe62f;</i>导出数据</a>

        <a class="layui-btn" id="importFlowers" href="javascript:void(0);" onclick="uploadExcel()"><i class="layui-icon">&#xe601;</i>导入数据</a>

        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </blockquote>
    <table id="flowersTable" lay-filter="flowersTable"></table>
</div>

<script>
    layui.use(['jquery', 'table', 'layer', 'util'], function(){
        var $ = layui.$, table = layui.table, layer = layui.layer, util = layui.util;
        var active = {
            reload: function(){
                table.reload('search_table_id', {
                    where: {
                        name: $('.searchContext').val(),
                        page: 1
                    }
                });
            }
        };

        //以数据表格形式显示
        table.render({
            elem: '#flowersTable'
            ,height: 450
            ,url: 'flowers/flowersTable' //数据接口
            ,page: true //开启分页
            ,id: 'search_table_id'
            ,cols: [[ //表头
                {field: 'id', title: '花卉ID', width:100, sort: true, fixed: 'left'}
                ,{field: 'name', title: '花卉名称', width:100}
                ,{field: 'color', title: '花卉颜色', width:110, sort: true}
                ,{field: 'fproperty', title: '花卉属性', width: 110}
                ,{field: 'detail', title: '花卉详情', width: 230}
                ,{field: 'image', title: '花卉图片', width: 110, sort: true, templet:'<div><img src="/{{d.image}}"></div>'}
                ,{field: 'fname', title: '花卉类型', width: 115, sort: true}
                ,{field: 'right', title: '操作', width:200, align:'center', toolbar: '#barDemo'}
            ]]
        });

        // 搜索（利用定义的active重新载入表格）
        $('.search_btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听工具条
        table.on('tool(flowersTable)', function(obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data, //获得当前行数据
                layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'detail') {
                layer.msg('查看操作');
                layer.open({
                    type : 2,
                    area : ['490px','450px'],
                    content : "flowers/readFlower?id="+data.id
                });
            } else if (layEvent === 'del') {
                layer.confirm('真的删除行么', function(index) {
                    obj.del(); //删除对应行（tr）的DOM结构
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        url: 'flowers/delFlower',
                        type: 'post',
                        data: {
                            id:data.id
                        },
                        success: function (result) {
                            alert(result);
                            window.location.reload();
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                layer.msg('编辑操作');
                layer.open({
                    type : 2,
                    area : ['430px','450px'],
                    content : "flowers/toFlowersUpdate?id="+data.id
                });
            }
        });

        //添加
        $(".addType_btn").on("click",function(){
            layer.open({
                title : "添加花卉信息",
                type : 2,
                area: ['415px', '450px'],
                content: "flowers/toFlowersAdd",
                success: function (layero, index) {
                    // 动态生成文章 下拉框
                    var body = layui.layer.getChildFrame('body', index);
                }
            });
        });
    });
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['jquery', 'upload'], function () {
        // 添加文件 上传服务器
        var $ = layui.$, upload = layui.upload;
        upload.render({
            elem: '#importFlowers',
            size: 300,
            exts: 'doc|docx|xls|xlsx',
            url: '/manage/flowers/importFlowers',
            done: function(result){
                layer.closeAll('loading');
                alert(result.data);
                window.location.reload();
            },
            error: function(result){
                layer.closeAll('loading');
                window.location.reload();
            }
        });
    });
</script>

</body>
</html>